package com.example.luke.shengdangxiaoattendance.Util;

import android.app.Activity;
import android.app.AlertDialog;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;

/**
 * Created by luke on 1/1/17.
 */

public class ZYReaderTool extends ZYScannerModel {
    private Activity activity;


    public void setMiReaderObjArrayList(ArrayList<MIReaderUtils.MIReaderObj> miReaderObjArrayList) {
        this.miReaderObjArrayList = miReaderObjArrayList;
    }



    public ZYReaderTool(Activity activity){


        this.activity=activity;


    }
    private AlertDialog alertDialog;

    void showCardDialog(String content){
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);


// 2. Chain together various setter methods to set the dialog characteristics
        builder.setMessage(content)
                .setTitle("M1卡信息");
        if (alertDialog==null){
            alertDialog=builder.create();
        }

        if (alertDialog.isShowing()){

            alertDialog.dismiss();
        }





        alertDialog.setMessage(content);
        alertDialog.show();



    }


    MIReaderUtils.MIReaderObj getCorrespondSectorObj(int sector){
        MIReaderUtils.MIReaderObj miReaderObj=new MIReaderUtils.MIReaderObj();
        for (MIReaderUtils.MIReaderObj temp:this.miReaderObjArrayList){

            if (temp.getSector().equals(sector)){
                miReaderObj=temp;

                break;

            }
        }
        return miReaderObj;

    }

    private String getMICardUserGender(){//卡user性别
        String value="女";

        try {

            value= MIReaderUtils.getStringAtIndex(getMiCardValue(READ_SECTOR+1,0),8);


            int remark= Integer.valueOf(MIReaderUtils.convertToDecimal(value));

            if (remark==1)
                value="女";

            else
                value="男";
        }

        catch (Exception e){

            GlobalFunction.printTextByTag(e.getMessage());

        }

        return value;


    }


    void showCardM1CardInfoContent(){//显示卡里信息


        String[]strings={"卡号","类型","姓名","性别","卡余"};
        String content="查不到卡信息";
        String temp="";
        try {
            String[]values={getMICardNo(),getMICardType(),getMIcardUserName(),getMICardUserGender(),getMICardBalance()+"元"};
            for (int i=0;i<strings.length;i++){
                String key=strings[i];
                String val=values[i];
                temp=temp+key+" : "+val+"\n";
            }



        }catch (Exception e){
            GlobalFunction.printTextByTag("parse error");
        }

        if (temp !="")
            content=temp;

        showCardDialog(content);

    }

    public String getMICardType(){//查看卡类

        String value="";

        try {

            ;

            value=MIReaderUtils.convertToDecimal(  MIReaderUtils.getStringAtIndex(  getMiCardValue(READ_SECTOR,2),4)) ;
           // value= CardTypeInfo.getInstance().getCorrespondCardTypeName(value);

        }
        catch (Exception e){

            GlobalFunction.ToastAlert(activity.getBaseContext(),"搜不到对应卡类");
            GlobalFunction.printTextByTag(e.getMessage());

        }
        return value;

    }

    public String getMIcardUserName() throws IOException {//卡user
        String value="";


        try {
            value= getMiCardValue(READ_SECTOR+1,2).substring(0, 32);

        }
        catch (Exception e){
            GlobalFunction.printTextByTag(e.getMessage());
        }
        return MIReaderUtils.convertFromChinese(value);

    }


    public String getMICardNo(){//卡号
        String value="";

        GlobalFunction.printTextByTag(getMiCardValue(READ_SECTOR,2));
        try {

            value=  getMiCardValue(READ_SECTOR, 2).substring(0, 4);
            String value2=getMiCardValue(READ_SECTOR, 2).substring(28, 30);
            value=value+value2;
            value=MIReaderUtils.convertHexPosition(value);


            //  GlobalFunction.printText("tag", value2);
        }
        catch (Exception e){
            GlobalFunction.printTextByTag(e.getMessage());
            GlobalFunction.printTextByTag("read card no error");
        }
        String finValue= MIReaderUtils.convertToDecimal(value);
        GlobalFunction.printTextByTag("finVal"+finValue);
        return finValue;



    }

    public String getMICardBalance(){//卡余额
        String value="";

        try {
            value=MIReaderUtils.getStringAtRange(getMiCardValue(READ_SECTOR,getBackUpIndex()),4,6);
            value=MIReaderUtils.convertHexPosition(value);

            String temp=MIReaderUtils.convertToDecimal(value);
            Integer integer= Integer.valueOf(temp);

            value= String.format(Locale.getDefault(),"%.2f", integer / 100.);

        }
        catch (Exception e){
            GlobalFunction.printTextByTag(e.getMessage());
        }

        return value;


    }



}
